package com.bw.zg2.view.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.zg2.R
import com.bw.zg2.base.BaseFragment
import com.bw.zg2.bean.GoodsTypeBean
import com.bw.zg2.bean.GoodsTypeItem
import com.bw.zg2.databinding.FragmentTypeBinding
import com.bw.zg2.view.activity.GoodsDetailActivity
import com.bw.zg2.view.adapter.TypeGoodsAdapter
import com.bw.zg2.view.adapter.TypeLeftAdapter
import com.bw.zg2.viewmodel.TypeViewModel
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter.OnItemClickListener

/**
 * create an instance of this fragment.
 */
class TypeFragment : BaseFragment<TypeViewModel, FragmentTypeBinding>(R.layout.fragment_type) {

    private lateinit var typeAdapter: TypeLeftAdapter
    private lateinit var goodsAdapter: TypeGoodsAdapter
    var typeList = mutableListOf<GoodsTypeBean>()
    var goodsList = mutableListOf<GoodsTypeItem>()
    override fun initData() {
        viewModel.typeBeanLiveData.observe(this) {
            //默认设置第一条数据选中
            it.data[0].isCheck = true

            typeList.clear()
            typeList.addAll(it.data)
            typeAdapter.notifyDataSetChanged()


            //默认展示第一条分类的商品列表
            goodsList.clear()
            dataBinding.tvTypeName.text = typeList[0].category_name
            goodsList.addAll(typeList[0].data)
            goodsAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {

        viewModel.getGoodsTypes()

        dataBinding.rvType.layoutManager = LinearLayoutManager(activity,LinearLayoutManager.HORIZONTAL,false)
        typeAdapter = TypeLeftAdapter(activity, R.layout.item_type_layout, typeList)
        dataBinding.rvType.adapter = typeAdapter


        //处理分类列表点击事件
        typeAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(p0: View?, p1: RecyclerView.ViewHolder?, p2: Int) {
                //设置选择状态
                for (i in typeList.indices) {
                    if (i == p2) {
                        typeList[p2].isCheck = true
                    } else {
                        typeList[i].isCheck = false
                    }
                }

                typeAdapter.notifyDataSetChanged()

                //根据分类列表选中下标，更新对应的商品列表
                dataBinding.tvTypeName.text = typeList[p2].category_name


                goodsList.clear()
                goodsList.addAll(typeList[p2].data)
                goodsAdapter.notifyDataSetChanged()

            }

            override fun onItemLongClick(
                p0: View?,
                p1: RecyclerView.ViewHolder?,
                p2: Int
            ): Boolean {
                return false
            }

        })

        dataBinding.rvGoods.layoutManager = GridLayoutManager(activity, 3)
        goodsAdapter = TypeGoodsAdapter(activity, R.layout.item_type_goods_layout, goodsList)
        dataBinding.rvGoods.adapter = goodsAdapter

        goodsAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(p0: View?, p1: RecyclerView.ViewHolder?, p2: Int) {
                val intent = Intent(activity, GoodsDetailActivity::class.java)
                intent.putExtra("goodsId", goodsList[p2 ].id)
                startActivity(intent)
            }

            override fun onItemLongClick(
                p0: View?,
                p1: RecyclerView.ViewHolder?,
                p2: Int
            ): Boolean {
                return false
            }

        })

    }


}